package com.gengzi.lot.item.controller;

import com.gengzi.lot.item.pojo.Menu;
import com.gengzi.lot.item.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 董诗攀
 */
@RestController
@RequestMapping("menus")
@Api(value = "MenuController")
public class MenuController {

    @Autowired
    private MenuService menuService;
    /**
     * 根据父id查询子节点
     * @param pid
     * @return
     */
    @GetMapping("list")
    @ApiOperation(notes = "根据父id查询子节点", value = "queryMenuByPid")
    public ResponseEntity<List<Menu>> queryMenuByPid(@RequestParam("pid") Long pid) {

        if (pid == null || pid.longValue() < 0) {
            // 响应400，相当于ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            return ResponseEntity.badRequest().build();
        }
        List<Menu> menuList = this.menuService.querymenuListByPid(pid);
        if (CollectionUtils.isEmpty(menuList)) {
            // 响应404
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(menuList);
    }

    @GetMapping("getList")
    @ApiOperation(notes = "获取菜单树", value = "queryList")
    public ResponseEntity<List<Menu>> queryList(){
        List<Menu> result = this.menuService.getTree();
        return ResponseEntity.ok(result);
    }

}
